package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmUserDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    /**
     * 登录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(WmUserDto dto) {
        //1.检查参数
        if (StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "用户名或密码错误");
        }
        //2.查询数据库中的用户信息
        LambdaQueryWrapper<WmUser> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmUser::getName, dto.getName());

        List<WmUser> list = list(lambdaQueryWrapper);
        if (list != null && list.size() == 1) {
            WmUser wmUser = list.get(0);
            String pwd= DigestUtils.md5DigestAsHex((dto.getPassword()+wmUser.getSalt()).getBytes());
            if (StringUtils.equals(pwd,wmUser.getPassword())){
                //登陆成功
                Map<String, Object> map = Maps.newHashMap();
                wmUser.setPassword("");
                wmUser.setSalt("");
                map.put("token", AppJwtUtil.getToken(wmUser.getApUserId().longValue()));
                map.put("user", wmUser);
                return ResponseResult.okResult(map);
            }else {
                //密码错误
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
    }

    @Override
    public ResponseResult findByName(String name) {
        LambdaQueryWrapper<WmUser> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmUser::getName,name);
        List<WmUser> list = list(lambdaQueryWrapper);
        if(list!=null && !list.isEmpty()){
            return ResponseResult.okResult(list.get(0));
        }
        return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    @Override
    public ResponseResult findById(Integer id) {
        LambdaQueryWrapper<WmUser> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmUser::getApUserId,id);
        List<WmUser> list = list(lambdaQueryWrapper);
        if(list!=null && !list.isEmpty()){
            return ResponseResult.okResult(list.get(0));
        }
        return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
}
